<template>
  <div class="login inline-block absolute" style="font-size: 0">
    <div class="form">
      <div class="item">
        <el-input class="mb-10px" v-model="tenantName" size="mini" placeholder="请输入学校">
          <template slot="prepend">学校</template>
        </el-input>
      </div>
      <div class="item">
        <el-input class="mb-10px" v-model="username" size="mini" placeholder="请输入学号">
          <template slot="prepend">学号</template>
        </el-input>
      </div>
      <div class="item">
        <el-input
          class="mb-10px"
          type="password"
          v-model="password"
          size="mini"
          placeholder="请输入密码"
        >
          <template slot="prepend">密码</template>
        </el-input>
      </div>
      <el-button class="item w-full" type="primary" size="mini" @click="login"> 登录 </el-button>
    </div>
  </div>
</template>

<script>
import { getTenantListByName, Login } from '@/api/login'

export default {
  components: {},
  data() {
    return {
      tenantName: '新学校',
      username: '0001',
      password: '123456' //dev:admin123,test:123456
    }
  },
  created() {},
  mounted() {},
  methods: {
    async login() {
      if (!this.tenantName) {
        alert('请输入学校')
        return
      }

      const { data: tenantList } = await getTenantListByName({ name: this.tenantName })

      if (!(Array.isArray(tenantList) && tenantList.length > 0)) {
        alert('请输入正确的学校名称')
        return
      }

      const tenantId = tenantList[0].id
      const username = this.username
      const password = this.password

      if (!username) {
        alert('请输入学号')
        return
      }

      if (!password) {
        alert('请输入密码')
        return
      }

      const { data: user, msg = '' } = await Login({
        tenantId,
        username,
        password
      })
      if (!user) {
        alert(msg)
      } else {
        let tokenStr = `tenantId=${tenantId}&`
        Object.keys(user).forEach((key) => {
          tokenStr += `${key}=${user[key]}&`
        })
        let tokenEnCode = window.btoa(tokenStr)
        this.$router.push(`/pages/index/index?token=${tokenEnCode}`)
        // this.$store.commit('setUser', user)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
button {
  line-height: 16px !important;
}

.login {
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .form {
    .item {
    }
  }
}
</style>
